﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using ConnectionControl;
using pokerServer.GameManagment.GameTools;

namespace pokerServer.ProtHandlers.Concrete {
	
	public class tableV2ProtHandle : IProtHandle {

		public Player myPlayer;
		public ConnectionManag cm;
		public string[] line;
		public ClientConnection cc;
		public Table table;

		public tableV2ProtHandle(ConnectionManag cm, ClientConnection cc, Player p, string[] s) {
			this.cm = cm;
			this.cc = cc;
			this.myPlayer = p;
			this.line = s;
		}

		public String work() {

			Table t = null;
			TableManage tMan = this.cm.tablem;
			try {
				//this.myPlayer.table.parseData(this.myPlayer, line.ToString());

				switch (line[0]) {
					case "JOINTABLE":
						t = this.cm.tablem.findTable(Convert.ToUInt32(line[1]));
						Console.WriteLine("Pppp --->" + this.myPlayer);
						t.joinTable(this.myPlayer);
						break;

					case "PARTTABLE":
						t = this.cm.tablem.findTable(Convert.ToUInt32(line[1]));
						t.partTable(this.myPlayer);
						break;

					case "SEATDOWNTABLE":
						t = this.cm.tablem.findTable(Convert.ToUInt32(line[1]));
						t.SeatDown(this.myPlayer, Convert.ToUInt16(line[2]), Convert.ToUInt32(line[3]));
						break;

					case "SEATUPTABLE":
						t = this.cm.tablem.findTable(Convert.ToUInt32(line[1]));
						t.PlaceOutSeat(this.myPlayer.Seat);
						break;

					case "ACTION":
						t = this.myPlayer.table;
						switch (line[1]) {
							case "FOLD":
								t.playerAction(this.myPlayer.Seat, GameEngine.GameDecisions.Fold);
								break;

							case "CHECK":
								t.playerAction(this.myPlayer.Seat, GameEngine.GameDecisions.Check);
								break;

							case "CALL":
								t.playerAction(this.myPlayer.Seat, GameEngine.GameDecisions.Call);
								break;

							case "RAISE":
								t.playerAction(this.myPlayer.Seat, GameEngine.GameDecisions.Raise, Convert.ToUInt32(line[2]));
								break;
						}
						return "ACTION-ACK";
						//break;

					default:
						break;
				}
				
			}
			catch (Exception e) {
				return string.Format("{0}-FAIL {1} {2}", line[0], t.TableID, e.Message);
			}

			return "";
		}
	}
}
